<template>
  <Form ref="form" :model="form" :rules="rule" :label-width="100">
    <FormItem label="文件名">
      <Upload
      name="file"
      action=""
      :format="['xls','xlsx']"
      :before-upload="handleUpload">
        <Button icon="ios-cloud-upload-outline">上传文件</Button>
      </Upload>
    </FormItem>
    <div v-if="file!=null">{{file}}</div>
  </Form>
  </template>
<script>
import { save, get } from '@/api/wzyy/wzyy.cloudbaebatch.service.js'
export default {
  props: {
    isEdit: {
      type: Boolean,
      default: false
    },
    params: {
      type: Object,
      default () {
        return {}
      }
    }
  },
  data () {
    return {
      file: null,
      form: {
        id: 0,
        fileName: ''
      },
      rule: {
      }
    }
  },
  mounted () {
    if (this.isEdit) {
      get({
        id: this.params.id
      }).then(res => {
        this.form = this.$util.copy(res.data, this.form)
      })
    }
  },
  methods: {
    handleUpload (file) {
      this.file = file
      return false
    },
    submit () {
      return new Promise((resolve, reject) => {
        if (this.file === null) {
          reject(new Error('文件不存在'))
        }
        let formData = new FormData()
        formData.append('file', this.file)
        console.log(formData)
        save(formData).then(res => {
          resolve(res)
        }).catch(e => {
          reject(e)
        })
      })
    }
  }
}
</script>
